<template>
	<view class="profile">
		<!-- 用户信息卡片 -->
		<view class="user-card" @click="nav('/pages/user/info')">
			<view class="user-info">
				<view class="info-content">
					<view class="main-info">
						<text class="username">{{userInfo.username}}</text>
						<text class="role">{{userInfo.is_admin ? '管理员' : '普通用户'}}</text>
					</view>
					<view class="sub-info">
						<text class="email">{{userInfo.email}}</text>
					</view>
				</view>
				<uni-icons type="right" size="16" color="#666"></uni-icons>
			</view>
		</view>

		<!-- 功能菜单 -->
		<uni-list>
			<uni-list-item title="账号设置" 
						   @click="nav('/pages/user/settings')"
						   showArrow />
			<uni-list-item title="关于我们" 
						   @click="nav('/pages/user/about')"
						   showArrow />
		</uni-list>

		<!-- 退出登录 -->
		<button class="logout-btn" type="warn" @click="handleLogout">退出登录</button>
	</view>
</template>

<script>
	import request from '@/utils/request.js'

	export default {
		data() {
			return {
				userInfo: {
					username: '',
					email: '',
					is_admin: false,
					created_at: ''
				}
			}
		},

		onShow() {
			this.getUserInfo()
		},

		methods: {
			nav(e) {
				uni.navigateTo({
					url: e
				})
			},
			// 获取用户信息
			async getUserInfo() {
				try {
					const res = await request({
						url: '/profile',
						method: 'GET'
					})
					if (res.data.status === 'success') {
						this.userInfo = res.data.data
						// 存储用户信息
						uni.setStorageSync('userInfo', this.userInfo)
						// 更新 tabBar 显示
						uni.setTabBarItem({
							index: 1,
							visible: this.userInfo.is_admin
						})
					}
				} catch (e) {
					console.error('获取用户信息失败:', e)
				}
			},

			// 格式化日期
			formatDate(date) {
				return new Date(date).toLocaleString()
			},

			// 页面跳转
			navigateTo(url) {
				if (url.includes('/pages/admin/')) {
					// 管理后台页面使用 redirectTo，避免返回按钮
					uni.redirectTo({
						url
					})
				} else {
					uni.navigateTo({
						url
					})
				}
			},

			// 退出登录
			handleLogout() {
				uni.showModal({
					title: '确认退出',
					content: '确定要退出登录吗？',
					success: (res) => {
						if (res.confirm) {
							// 清除登录状态
							uni.removeStorageSync('token')
							uni.removeStorageSync('userId')
							uni.removeStorageSync('userInfo')

							// 跳转到登录页
							uni.reLaunch({
								url: '/pages/auth/login'
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.profile {
		.user-card {
			margin: 20rpx;
			background-color: #fff;
			border-radius: 12rpx;
			padding: 30rpx;
			box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);

			.user-info {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.info-content {
					.main-info {
						.username {
							font-size: 32rpx;
							font-weight: bold;
							color: #333;
						}

						.role {
							font-size: 24rpx;
							color: #666;
							margin-left: 16rpx;
							background-color: #f5f5f5;
							padding: 4rpx 12rpx;
							border-radius: 20rpx;
						}
					}

					.sub-info {
						margin-top: 12rpx;

						.email {
							font-size: 24rpx;
							color: #666;
						}
					}
				}
			}
		}

		.logout-btn {
			margin: 50rpx 30rpx;
		}
	}
</style>